﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using StyleMVVM.Data;
using StyleMVVM.DependencyInjection;
#if DOT_NET
using StyleMVVM.Utilities;
#else
using Windows.Foundation.Metadata;
#endif
#if NETFX_CORE
using Windows.UI;
using Windows.UI.Xaml.Media;

#else
using System.Windows.Media;

#endif

namespace StyleMVVM.View.Impl
{
	/// <summary>
	/// This is a 
	/// </summary>
	public sealed class FauxNavigationService : IFauxNavigationService, INavigationService
	{
		private Brush overlayBrush = new SolidColorBrush(Colors.Red);
		private string overlayName = null;

		public static void RegisterExport(IDependencyInjectionContainer container)
		{
			container.Register(typeof(FauxNavigationService)).
			          As(typeof(INavigationService)).
			          As(typeof(IFauxNavigationService)).
			          InEnvironment(ExportEnvironment.UnitTestOnly | ExportEnvironment.DesignTimeOnly).AndShared();
		}

		public FauxNavigationService()
		{
		}

		public FauxNavigationService(object frame)
		{
		}

		public bool? AllowBack { get; set; }

		public bool? AllowFoward { get; set; }

		public bool? AllowNavigate { get; set; }

		public bool IsValid
		{
			get { return true; }
		}

		public bool CanGoBack
		{
			get
			{
				if (AllowBack.HasValue)
				{
					return AllowBack.Value;
				}

				return true;
			}
		}

		public bool CanGoForward
		{
			get { return true; }
		}

		public void GoHome()
		{
		}

		public void GoBack()
		{
		}

		public void GoForward()
		{
		}

#if !WINDOWS_PHONE
		[DefaultOverload]
		public bool Navigate(string pageName)
		{
			return Navigate(pageName, null);
		}

		public bool Navigate(Type pageType)
		{
			return Navigate(pageType, null);
		}

		[DefaultOverload]
		public bool Navigate(string pageName, object navigationParam)
		{
			if (AllowNavigate.HasValue)
			{
				return AllowNavigate.Value;
			}

			return true;
		}

		public bool Navigate(Type pageType, object navigationParam)
		{
			if (AllowNavigate.HasValue)
			{
				return AllowNavigate.Value;
			}

			return true;
		}
#else
		public bool Navigate(Uri pageName)
		{
			if (AllowNavigate.HasValue)
			{
				return AllowNavigate.Value;
			}

			return true;
		}
#endif

		public Brush OverlayBackground
		{
			get { return overlayBrush; }
			set
			{
				if (overlayBrush != value)
				{
					overlayBrush = value;

					OnPropertyChanged();
				}
			}
		}

		public bool OverlayOpen
		{
			get { return overlayName != null; }
		}

		public bool OpenOverlay(string overlayName)
		{
			return OpenOverlay(overlayName, null);
		}

		public bool OpenOverlay(string overlayName, object context)
		{
			if (overlayName != null && this.overlayName == null)
			{
				this.overlayName = overlayName;

				OnPropertyChanged("CanGoBack");
				OnPropertyChanged("CanGoForward");
				OnPropertyChanged("OverlayOpen");

				return true;
			}

			return false;
		}

		public bool ReplaceOverlay(string newOverlayName)
		{
			return ReplaceOverlay(overlayName, null);
		}

		public bool ReplaceOverlay(string newOverlayName, object context)
		{
			if (overlayName != null)
			{
				this.overlayName = newOverlayName;

				return true;
			}

			return false;
		}

		public bool CloseOverlay()
		{
			bool returnValue = false;

			if (overlayName != null)
			{
				overlayName = null;

				returnValue = true;

				OnPropertyChanged("CanGoBack");
				OnPropertyChanged("CanGoForward");
				OnPropertyChanged("OverlayOpen");
			}

			return returnValue;
		}

		public event PropertyChangedEventHandler PropertyChanged;

		private void OnPropertyChanged([CallerMemberName] string propertyName = null)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}